// components/cart/cart.ts
Component({

  /**
   * 组件的属性列表
   */
  properties: {
    cartArr: {
      type: Array,
      value: []
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    total: 0,
    isShowList: false,
    deleteArr: [],
    check:true

  },

  observers: {
    'cartArr': function (arr) {
      const total = arr.reduce((pre, item) => {
        return pre + (item.num * item.price)
      }, 0)
      this.setData({
        total
      })
      wx.setStorageSync('cart', arr)

      // wx.setStorageSync('cart', JSON.stringify(arr))

    }
  },

  /**
   * 组件的方法列表
   */
  methods: {
    handleClickCart(e) {
      console.log(e.touches[0].clientX,e.touches[0].clientY)
      
      this.setData({
        isShowList: !this.data.isShowList
      })
    },
    handleUpdateCart(e) {
      const num = e.detail;
      const {
        index
      } = e.currentTarget.dataset
      this.triggerEvent('handleUpdateCart', {
        index,
        num
      })
    },

    handleSelectChange(e) {
      this.setData({
        deleteArr:e.detail,
      });
    },

    handleDelete(){
      this.triggerEvent('handleDelete', this.data.deleteArr)
      this.setData({
        deleteArr:[]
      })

    },
    toPay(){
      if(this.properties.cartArr.length===0){
        wx.showModal({
          title: '提示',
          content: '没有任何商品',
          showCancel:false,
        })
      }else{
        const orderType = wx.getStorageSync('orderType')
        if(orderType==0){
          wx.navigateTo({
            url: '/pages/user/takeoutOrderConfirm/orderConfirm',
          })
        }else if(orderType==1){
          wx.navigateTo({
            url: '/pages/user/monthOrderConfirm/orderConfirm',
          })
        }
        
      }
    }

  }
})